package com.quasar.hpatient.module.mine_phone;

import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.quasar.hpatient.R;

import lib.quasar.base.frame.BaseActivity;
import lib.quasar.widget.menu.NavigationView;
import lib.quasar.widget.timer.TimerView;

/**
 * description: 修改手机号
 * created by kalu on 2018/5/17 13:37
 */
public class MinePhoneActivity extends BaseActivity<MinePhonePresenter> implements MinePhoneView {

    @Override
    public int initView() {
        return R.layout.activity_mine_phone;
    }

    @Override
    public void initDataLocal() {

        final NavigationView menu = findViewById(R.id.mine_phone_menu);
        menu.setOnNavigationChangeListener((isBack, isMenu) -> {
            if (isBack) onBackPressed();
            else {
                final TextView password = findViewById(R.id.mine_phone_input);
                final TextView phone = findViewById(R.id.mine_phone_number);
                final TextView code = findViewById(R.id.mine_password_code);
                getPresenter().modifyPhone(MinePhoneActivity.this, code.getText().toString(), password.getText().toString(), phone.getText().toString());
            }
        });

        final TextView button = findViewById(R.id.mine_phone_qrcode);
        final TimerView timer = findViewById(R.id.mine_phone_timer);
        button.setOnClickListener(v -> {
            final TextView phone = findViewById(R.id.mine_phone_number);
            getPresenter().sendCode(MinePhoneActivity.this, timer, button, phone.getText().toString());
        });

        findViewById(R.id.mine_phone_eye).setOnClickListener(v -> changeInputType(R.id.mine_phone_input, R.id.mine_phone_eye));
    }

    @Override
    public void changeInputType(int editId, int imageId) {
        EditText input = findViewById(editId);
        if (null == input || input.getText().length() == 0) return;

        ImageView image = findViewById(imageId);
        if (null == image) return;

        boolean ok = (input.getTransformationMethod() == PasswordTransformationMethod.getInstance());
        image.setImageDrawable(getResources().getDrawable(ok ? R.drawable.ic_comm_regist_password_on : R.drawable.ic_comm_regist_password_off));
        input.setTransformationMethod(ok ? HideReturnsTransformationMethod.getInstance() : PasswordTransformationMethod.getInstance());
        input.setSelection(input.getText().length());
    }
}